// import { login, getInfo } from '@/api/login'
import { login } from '@/api/login'
import { Message } from 'element-ui'
import router, { resetRouter } from '@/router'

const state = {
  token: localStorage.getItem('token') ? localStorage.getItem('token') : '', // 认证凭证'
  userName: '张三',
  roles: [],
  introduce: ''
}
const mutations = {
  SET_TOKEN(state, val) {
    state.token = val
    localStorage.setItem('token', val)
  },
  DEL_TOKEN(state) {
    state.token = ''
    state.userName = ''
    state.roles = ''
    state.introduce = ''
    localStorage.removeItem('token')
  },
  SET_ROLES(state, payload) {
    state.roles = payload
  },
  SET_NAME(state, payload) {
    state.userName = payload
  },
  SET_INTRODUCE(state, payload) {
    state.introduce = payload
  }
}
const actions = {
  // user login
  _login({ commit }, formdatas) {
    return new Promise((resolve, reject) => {
      login(formdatas)
        .then(res => {
          if (res.code === 0) {
            if (res.data.success) {
              Message.success(res.data.msg)
              commit('SET_TOKEN', res.data.token)
            } else {
              Message.error(res.data.msg)
            }
            resolve(res)
          }
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  loginOut({ commit }) {
    commit('DEL_TOKEN')
    resetRouter()
    router.push({
      path: '/login',
      query: {
        redirect: '/'
      }
    })
  },
  _getInfo({ commit }) {
    //  return new Promise((resolve, reject) => {
    //     getInfo()
    //       .then(res => {
    //         if (res.code === 0) {

    const { name, roles, introduce } = {
      name: '颤三',
      roles: [
        'Home',
        'Dashbord',
        'Driver',
        'Driver-index',
        'Permission',
        'PageUser',
        'PageAdmin',
        'Table',
        'BaseTable',
        'ComplexTable',
        'Icons',
        'Icons-index',
        'Components',
        'Sldie-yz',
        'Upload',
        'Carousel',
        'Echarts',
        'Sldie-chart',
        'Dynamic-chart',
        'Map-chart',
        'Excel',
        'Excel-out',
        'Excel-in',
        'Mutiheader-out',
        'Error',
        'Page404',
        'Github',
        'NavTest',
        'Nav1',
        'Nav2',
        'Nav2-1',
        'Nav2-2',
        'Nav2-2-1',
        'Nav2-2-2',
        '*404'
      ],
      introduce: '描述'
    }
    commit('SET_ROLES', roles)
    commit('SET_NAME', name)
    commit('SET_INTRODUCE', introduce)
    return roles
    //         } else {
    //           Message.error(res.msg)
    //         }
    //         resolve(res.data)
    //       })
    //       .catch(error => {
    //         reject(error)
    //       })
    //   })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
